package com.ruoyi.web.controller.production.greenhousefilm;

import java.sql.SQLIntegrityConstraintViolationException;
import java.util.List;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.production.domain.greenhousefilm.GreenhousefilmType;
import com.ruoyi.production.service.greenhousefilm.IGreenhousefilmTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 棚膜类型(长*宽)Controller
 *
 * @author ruoyi
 * @date 2021-07-12
 */
@RestController
@RequestMapping("/production/greenhousefilmType")
public class GreenhousefilmTypeController extends BaseController
{
    @Autowired
    private IGreenhousefilmTypeService greenhousefilmTypeService;

    /**
     * 查询棚膜类型(长*宽)列表
     */
    @PreAuthorize("@ss.hasPermi('production:greenhousefilmType:list')")
    @GetMapping("/list")
    public TableDataInfo list(GreenhousefilmType greenhousefilmType)
    {
        startPage();
        List<GreenhousefilmType> list = greenhousefilmTypeService.selectGreenhousefilmTypeList(greenhousefilmType);
        return getDataTable(list);
    }

    /**
     * 导出棚膜类型(长*宽)列表
     */
    @PreAuthorize("@ss.hasPermi('production:greenhousefilmType:export')")
    @Log(title = "棚膜类型(长*宽)", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(GreenhousefilmType greenhousefilmType)
    {
        List<GreenhousefilmType> list = greenhousefilmTypeService.selectGreenhousefilmTypeList(greenhousefilmType);
        ExcelUtil<GreenhousefilmType> util = new ExcelUtil<GreenhousefilmType>(GreenhousefilmType.class);
        return util.exportExcel(list, "棚膜类型(长*宽)数据");
    }

    /**
     * 获取棚膜类型(长*宽)详细信息
     */
    @PreAuthorize("@ss.hasPermi('production:greenhousefilmType:query')")
    @GetMapping(value = "/{greenhousefilmTypeId}")
    public AjaxResult getInfo(@PathVariable("greenhousefilmTypeId") Long greenhousefilmTypeId)
    {
        return AjaxResult.success(greenhousefilmTypeService.selectGreenhousefilmTypeById(greenhousefilmTypeId));
    }

    /**
     * 新增棚膜类型(长*宽)
     */
    @PreAuthorize("@ss.hasPermi('production:greenhousefilmType:add')")
    @Log(title = "棚膜类型(长*宽)", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody GreenhousefilmType greenhousefilmType)
    {
        try {
            return toAjax(greenhousefilmTypeService.insertGreenhousefilmType(greenhousefilmType));
        }catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return AjaxResult.error("名称已存在！");
            }else return error("失败!");
        }

    }

    /**
     * 修改棚膜类型(长*宽)
     */
    @PreAuthorize("@ss.hasPermi('production:greenhousefilmType:edit')")
    @Log(title = "棚膜类型(长*宽)", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody GreenhousefilmType greenhousefilmType)
    {
        try {
            return toAjax(greenhousefilmTypeService.updateGreenhousefilmType(greenhousefilmType));
        }catch (Exception e) {
            if(e.getCause() instanceof SQLIntegrityConstraintViolationException) {
                return AjaxResult.error("名称已存在！");
            }else return error("失败!");
        }

    }

    /**
     * 删除棚膜类型(长*宽)
     */
    @PreAuthorize("@ss.hasPermi('production:greenhousefilmType:remove')")
    @Log(title = "棚膜类型(长*宽)", businessType = BusinessType.DELETE)
	@DeleteMapping("/{greenhousefilmTypeIds}")
    public AjaxResult remove(@PathVariable Long[] greenhousefilmTypeIds)
    {
        return toAjax(greenhousefilmTypeService.deleteGreenhousefilmTypeByIds(greenhousefilmTypeIds));
    }
}
